package com.csvw.skr.basic.service;

import com.google.common.collect.Lists;

import com.webonn.frame.db.MyDao;
import com.csvw.skr.basic.entity.*;
import com.webonn.frame.util.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2016/9/18.
 */
@Service
public class RbacService {
    @Autowired
    private MyDao dao;

    public List<User> queryUsers() {
        return dao.list("select * from csvw_skr_basic_user where active=true order by created asc ", User.class, new Object[]{});
    }

    public Map<String, Object> getUserByUserName(String username) {
        String get_user_sql = "select t_user.pk_empno as empno, t_user.name, t_user.phone, t_user.role, t_user.organization, organization.name as organizationName, " +
                "t_user.email, t_user.job, job.name as jobTitle, t_user.position, position.name as positionName, organization.name as organizationShortName, " +
                "IFNULL((SELECT oldcode FROM csvw_skr_basic_organization WHERE t_user.organization LIKE CONCAT('%',pk_code,'%') AND headquarters = 2 AND LEVEL = 2), oldcode) AS rboOldcode " +
                "from csvw_skr_basic_user t_user " +
                "left join csvw_skr_basic_job job on job.pk_code=t_user.job " +
                "left join csvw_skr_basic_position position on position.pk_code=t_user.position " +
                "left join csvw_skr_basic_organization organization on organization.pk_code=t_user.organization " +
                "where t_user.active=true and (t_user.pk_empno=? or t_user.email=? or t_user.phone = ?)  ";
        List<Map<String, Object>> result = Lists.newArrayList();
        result.addAll(dao.queryResultToMap(get_user_sql, new Object[]{username, username, username}));
        if (result.size() > 0) {
            return result.get(0);
        } else {
            return null;
        }
    }

    public User checkUser(String username) {
        String sql = "select * from csvw_skr_basic_user t_user where t_user.pk_empno=? or t_user.email=? or t_user.phone = ? or  t_user.email=concat(?,'@csvw.com')";
        List<User> users = dao.list(sql, User.class, new Object[]{username, username, username, username});
        return users != null && users.size() > 0 ? users.get(0) : null;
    }

    public User getUser(String empno) {
        return dao.get(empno, User.class);
    }

    public User updateUser(User user) {
        return dao.persist(user);
    }

    public User updateUserInfo(User user) {
        User user_old = getUser(user.getEmpno());
        user.setEmpno(user_old.getEmpno());
        user.setCreated(user_old.getCreated());
        user.setActive(user_old.isActive());
        user.setPassword(user_old.getPassword());
        user.setUpdated(new Date());
        return dao.persist(user);
    }

    public User updateUserPassword(String empno, String password) {
        User user = getUser(empno);
        user.setPassword(PasswordUtils.enpassword(password));
        return dao.persist(user);
    }

    public LoginLog addLoginLog(LoginLog entity) {
        entity.setCreated(new Date());
        entity.setId(null);
        return dao.persist(entity);
    }
}
